package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.domain.ResultEntity;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.service.DicTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 郭俊才
 * 2021/8/16
 */
@Controller
public class DicTypeController {
    @Autowired
    private DicTypeService dicTypeService;
    private Logger logger = LoggerFactory.getLogger(DicTypeController.class);

    @RequestMapping("settings/dictionary/type/index.do")
    public String getDicTypes(ModelMap modelMap){
        List<DicType> dicTypeList = dicTypeService.queryAll();
        modelMap.addAttribute("dicTypeList",dicTypeList);
        return "settings/dictionary/type/index";
    }
    @RequestMapping("settings/dictionary/type/checkCode.do")
    @ResponseBody
    public ResultEntity checkCode(String code){
        DicType dicType = dicTypeService.queryByPrimaryKey(code);
        if(dicType == null){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("编码已存在");
        }
    }
    //添加
    @RequestMapping("settings/dictionary/type/save.do")
    @ResponseBody
    public ResultEntity createDicType(DicType dicType){
        int saveNum = dicTypeService.save(dicType);
        if(saveNum == 1){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("添加失败");
        }
    }
    //修改页面回显数据
    @RequestMapping("settings/dictionary/type/getDicType.do")
    public String getDicType(String code,ModelMap modelMap){
        DicType dicType = dicTypeService.queryByPrimaryKey(code);
        modelMap.addAttribute("dicType",dicType);
        return "settings/dictionary/type/edit";
    }
    //修改
    @RequestMapping("settings/dictionary/type/edit.do")
    @ResponseBody
    public ResultEntity changeDicType(DicType dicType){
        int i = dicTypeService.editByPrimaryKey(dicType);
        if(i == 1){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("修改失败");
        }
    }
    //删除
    @RequestMapping("settings/dictionary/type/remove.do")
    @ResponseBody
    public ResultEntity dropDicType(@RequestBody String[] codes){
        int i = dicTypeService.removeByCodes(codes);
        if(i > 0){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("删除失败");
        }
    }
}